package com.coders.android.hibernate.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.coders.android.hibernate.dao.LoginDAO;
import com.coders.android.hibernate.dto.Login;

public class LoginDAOHibernate extends HibernateDaoSupport implements LoginDAO {

	@Override
	public boolean insertar(Login login) {

		Transaction transaction = null;
		try{
			Session sesion = getSession();
			
			transaction = sesion.beginTransaction();
			sesion.save( login );
			transaction.commit();
			return true;
			
		}catch (HibernateException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public String obtenerPassword(String cedula) {

		Login login = null;
		try{
			Session sesion = getSession();
			
			login = (Login) sesion.createCriteria( Login.class ).add( Restrictions.eq( "cedula", cedula ) ).uniqueResult();
			return login.getPassword();
			
		}catch (HibernateException e) {
			e.printStackTrace();
		}
		return null;
	}
}
